﻿//
//  HFHttpHtFactory.m
//  HFBaseLibModule
//
//  Created by Points on 2020/4/3.
//  Copyright © 2020 hfqf123@126.com. All rights reserved.
//

#import "HFHttpHtFactory.h"

@implementation HFHttpHtFactory

- (COPromise<COTuple *> *(^)(void))coPost{
    __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        BOOL needToast = weakSelf.needToast;
        BOOL rsaSwitch = weakSelf.rsaSwitch;
        HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
        HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
        NSDictionary *para = weakSelf.mPara;
        [HFBaseHTTPRequestManager HTPOST:weakSelf.mPath
                               parameters:weakSelf.mPara
                                  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            [HFHttpHtFactory requestSuccessWithTask4Cobjc:task
                                                body:para
                                      responseObject:responseObject
                                      successHandler:successBlock
                                           needToast:needToast
                                           rsaSwitch:rsaSwitch
                                            promise:weakSelf.promise];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            [HFHttpHtFactory requestFailureWithTask4Cobjc:task
                                                     param:weakSelf.mPara
                                                     error:error
                                            failureHandler:failBlock
                                                 needToast:needToast
                                                   promise:weakSelf.promise];
        }];
        return weakSelf.promise;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))post {
    
    __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        if (weakSelf.rsaEncryptResult){
            BOOL needToast = weakSelf.needToast;
            BOOL rsaSwitch = weakSelf.rsaSwitch;
            HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
            HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
            NSDictionary *para = weakSelf.mPara;
            NSURLSessionTask *task = [HFBaseHTTPRequestManager HTPOST:weakSelf.mPath
                                                            parameters:weakSelf.mPara
                                                               success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                [HFHttpHtFactory requestSuccessWithTask:task
                                                    body:para
                                          responseObject:responseObject
                                          successHandler:successBlock
                                               needToast:needToast
                                               rsaSwitch:rsaSwitch];
            } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                [HFHttpHtFactory requestFailureWithTask:task
                                                   param:weakSelf.mPara
                                                   error:error
                                          failureHandler:failBlock
                                               needToast:needToast];
            }];
            [weakSelf startDelayTimer:task];
        }
       
        return weakSelf;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))get {
    __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        BOOL needToast = weakSelf.needToast;
        HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
        HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
        NSDictionary *para = weakSelf.mPara;
        [HFBaseHTTPRequestManager HTGET:weakSelf.mPath parameters:weakSelf.mPara success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            [HFHttpHtFactory requestSuccessWithTask:task
                                                body:para
                                      responseObject:responseObject
                                      successHandler:successBlock
                                           needToast:needToast
                                           rsaSwitch:weakSelf.rsaSwitch];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            [HFHttpHtFactory requestFailureWithTask:task
                                               param:weakSelf.mPara
                                               error:error
                                      failureHandler:failBlock
                                           needToast:needToast];
        }];
        return weakSelf;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))singleUpload {
   __block __weak typeof(self) weakSelf = self;
    return ^(void) {
        BOOL needToast = weakSelf.needToast;
        HFHttpSucceedBlock successBlock = weakSelf.mSucceedBlock;
        HFHttpFailedBlock failBlock = weakSelf.mFailedBlock;
        BOOL rsaSwitch = weakSelf.rsaSwitch;
        NSDictionary *para = weakSelf.mPara;
        [HFBaseHTTPRequestManager HTPOST:weakSelf.mPath parameters:weakSelf.mPara constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:weakSelf.mImage name:@"file" fileName:weakSelf.mImageName?:@"image" mimeType:weakSelf.mMimeType?:@"image/png"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            [HFHttpHtFactory requestSuccessWithTask:task
                                                body:para
                                      responseObject:responseObject
                                      successHandler:successBlock
                                           needToast:needToast
                                           rsaSwitch:rsaSwitch];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            [HFHttpHtFactory requestFailureWithTask:task
                                               param:weakSelf.mPara
                                               error:error
                                      failureHandler:failBlock
                                           needToast:needToast];
        }];
        return weakSelf;
    };
}

- (id<HFHttpAbstractFactory> (^)(void))multiUpload {
    __weak typeof(self) weakSelf = self;
    return ^(void) {
        return weakSelf;
    };
}


@end

